# -*- coding:utf-8 -*-
# created on 2017/3/6

from mathsolver.functions.base import *
from mathsolver.functions.fushu.base import Complex
from mathsolver.functions.mathematica.mathematicaSolve import MathematicaSolve
from mathsolver.functions.fushu.basic import fs_search_multiple
from mathsolver.functions.fushu.FuShu_JieFangCheng import FSSolveEq


class FSFuShuSDuiChengUpdate(BaseFunction):
    """
    设复数z_{2},z_{2}在复平面内的对应点关于虚轴对称,z_{2}=2+i,则z_{2}z_{2}=()
    """
    def solver(self, *args):
        if len(args) == 3 and isinstance(args[0], BaseMultiple):
            poly1, poly2 = args[0].sympify()
            text = args[1]
            if isinstance(args[2], BaseComplexEq):
                solutions = FSSolveEq().solver(args[2]).output[0].sympify()
                name = list(solutions.keys())[0]
                value = solutions[name][0]
                comm = Complex(value)
                imag = comm.imag
                real = comm.real
                if str(name) == str(poly1):
                    if isinstance(text, str):
                        if text.find("虚轴") >= 0:
                            new_imag = imag
                            new_real = - real
                        elif text.find("实轴") >= 0:
                            new_imag = - imag
                            new_real = real
                        else:
                            raise Exception("to do")
                        new_poly2 = new_real + new_imag * sympify('I')
                        self.output.append(BaseComplexValue({(poly1, poly2): [(new_poly2, value)]}))
                        self.label.add("复数在复平面的点关于虚轴对称")
                    elif isinstance(text, BasePoint):
                        point = text.sympify()
                        new_real = 2 * point[0] - real
                        new_imag = 2 * point[1] - imag
                        new_poly2 = new_real + new_imag * sympify('I')
                        self.output.append(BaseComplexValue({(poly1, poly2): [(new_poly2, value)]}))
                    else:
                        raise Exception("to do")
                elif str(name) == str(poly2):
                    if text.find("虚轴") >= 0:
                        new_imag = imag
                        new_real = - real
                    elif text.find("实轴") >= 0:
                        new_imag = - imag
                        new_real = real
                    else:
                        raise Exception("to do")
                    new_poly1 = new_real + new_imag * sympify('I')
                    self.output.append(BaseComplexValue({(poly1, poly2): [(new_poly1, value)]}))
                    self.label.add("复数在复平面的点关于实轴对称")
        return self


class FSImagUpdate(BaseFunction):
    """
    设复数z的幅角的主值为\\frac{{2π}}{3},虚部为\\sqrt{3},则z^{2}=( )
    """
    def solver(self, *args):
        target = args[0].sympify()
        imag = args[1].sympify()
        target_value = self.search(target)
        comm = Complex(target_value)
        new_imag = comm.imag
        self.steps.append(["", "∴%s" % (BaseEq([imag, new_imag]).printing())])
        stepsolver = MathematicaSolve().solver(BaseEq([imag, new_imag]))
        solutions = stepsolver.output[0]
        self.steps.append(["", "解得: %s" % (solutions.printing())])
        answer = fs_search_multiple(solutions.sympify(), target_value)
        target_values = []
        for ans in answer:
            target_value = target_value.subs(ans)
            target_value = target_value.expand().simplify()
            target_values.append(target_value)
            self.output.append(BaseComplexValue({target: target_values}))
        return self
